<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\DrinkCategory;
use App\Http\Controllers\Api\V1\Resources\DrinkCategoryResource;
use App\Services\ApplyDrinkProductOrder\ApplyDrinkProductOrderRequest;
use App\Services\ApplyDrinkProductOrder\ApplyDrinkProductOrder;
use App\Http\Controllers\Api\V1\Resources\OrderResource;
use Illuminate\Support\Facades\Cache;

class DrinkController extends Controller
{

	public function take()
	{
		$lock = Cache::lock('take_drink_product', 15);

		try {

			if (! $lock->get()) fail('高峰拥挤，请再试');

			$request = app(ApplyDrinkProductOrderRequest::class);
			

			$order = app(ApplyDrinkProductOrder::class, [
			    'user' => $request->user(),
			    'address' => $request->getAddress(),
			    'product' => $request->getProduct(),
			    'attr' => $request->attr
		    ])->apply();
		    
            return new OrderResource($order);

		} finally {
			$lock->release();
		}
	}
}
